package com.jnu.myfirstapplication.Adapter;

import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jnu.myfirstapplication.CommentPage;
import com.jnu.myfirstapplication.R;
import com.jnu.myfirstapplication.data.Comment;
import com.jnu.myfirstapplication.data.MyImageView;

public class CommentRecycleViewAdapter extends RecyclerView.Adapter<CommentRecycleViewAdapter.ViewHolder>{


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.comment, parent, false);
        return new ViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        Comment comment = CommentPage.comments.get(position);
        holder.getComment().setText(comment.getComment());
        holder.getUsername().setText(comment.getUsername());
//        holder.getImageView()
        System.out.println(comment.getImageUrl());
        holder.getImageView().setImageURL(comment.getImageUrl());
//        holder.getImageView().setImageURL("https://pic.cnblogs.com/avatar/1142647/20170416093225.png");

        holder.getTime().setText(comment.getTime());

//        holder.setImageView(comment.getImageUrl());

//        Book book = BookListMainActivity.mFilterList.get(position);
//        Drawable drawable = null;
//        try {
//            String path = "/sdcard/Pictures/book/book" + book.getCoverResourceId() + ".jpg";
//            if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
//                ActivityCompat.requestPermissions((Activity)context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
//            }
//            Bitmap bitmap = BitmapFactory.decodeFile(path);
//            drawable = new BitmapDrawable(context.getResources(), bitmap);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        holder.getId().setImageDrawable(drawable);
//        holder.getName().setText(book.getTitle());
//        if (book.getAuthor().length() != 0) {
//            holder.getDescribe().setText("" + book.getAuthor() + " 著， " + book.getPublisher());
//        } else {
//            holder.getDescribe().setText("" + book.getPublisher());
//        }
//        holder.getPubDate().setText("" + book.getPubDate());
//        holder.

    }

    /**
     * Returns the total number of items in the data set held by the adapter.
     *
     * @return The total number of items in this adapter.
     */
    @Override
    public int getItemCount() {
        return CommentPage.comments.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener {
        MyImageView imageView;
        TextView username;
        TextView time;

        public MyImageView getImageView() {
            return imageView;
        }

        public void setImageView(MyImageView imageView) {
            this.imageView = imageView;
        }

        public TextView getUsername() {
            return username;
        }

        public void setUsername(TextView username) {
            this.username = username;
        }

        public TextView getTime() {
            return time;
        }

        public void setTime(TextView time) {
            this.time = time;
        }

        public TextView getComment() {
            return comment;
        }

        public void setComment(TextView comment) {
            this.comment = comment;
        }

        TextView comment;




        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.comment_img);
            username = itemView.findViewById(R.id.comment_username);
            time = itemView.findViewById(R.id.comment_time);
            comment = itemView.findViewById(R.id.comment_text);
            itemView.setOnCreateContextMenuListener(this);
        }



        @Override
        public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
//            contextMenu.add(0, MENU_ID_UPDATE, getAdapterPosition(), "Update" + getAdapterPosition());
//            contextMenu.add(0, MENU_ID_DELETE, getAdapterPosition(), "Delete" + getAdapterPosition());
        }

    }
}
